<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">

      <el-form-item label="领用人姓名" prop="receiverName">
        <el-input
          v-model="queryParams.receiverName"
          placeholder="请输入领用人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人姓名" prop="responsibleUserName">
        <el-input
          v-model="queryParams.responsibleUserName"
          placeholder="请输入负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-select v-model="queryParams.warehouseName" placeholder="请输入仓库名称">
          <el-option
            v-for="item in warehouseList"
            :key="item.id"
            :label="item.warehouseName"
            :value="item.warehouseName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="领用时间" prop="collectingTime">
        <el-date-picker clearable
          v-model="queryParams.collectingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择领用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:outbound:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:outbound:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:outbound:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:outbound:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :border="true" v-loading="loading" :data="outboundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="领用人姓名" align="center" prop="receiverName"  v-if="columns[0].visible"/>
      <el-table-column label="负责人姓名" align="center" prop="responsibleUserName" v-if="columns[1].visible"/>
      <el-table-column label="领用时间" align="center" prop="collectingTime" width="180" v-if="columns[2].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[3].visible"/>
      <el-table-column label="出库详情" align="center"  v-if="columns[4].visible">
         <template slot-scope="scope">
           <el-button type="primary" @click="details(scope.row)">出库详情</el-button>
         </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:outbound:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:outbound:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出库记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
<!--        <el-form-item label="领用人id" prop="receiverId">-->
<!--          <el-input v-model="form.receiverId" placeholder="请输入领用人id" />-->
<!--        </el-form-item>-->
        <el-form-item label="领用人姓名" prop="receiverName">
          <el-input style="width: 200px;" v-model="form.receiverName" placeholder="请输入领用人姓名" >
            <el-button slot="append" icon="el-icon-user" @click="handleStaff(0)" ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="responsibleUserName">
           <el-input style="width: 200px;" v-model="form.responsibleUserName"placeholder="请输入负责人姓名" >
            <el-button slot="append" icon="el-icon-user-solid" @click="handleStaff(1)"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="领取物品" prop="bizOutboundProduct">
          <el-button type="primary"  @click="handleProduct" >选取产品</el-button>
        </el-form-item>
        <el-table  :border="true" :data="form.bizOutboundProductList" style="margin-bottom: 10px;" >
          <el-table-column type="index" width="55" align="center"  />
          <el-table-column label="产品名称" align="center" prop="productName"  />
          <el-table-column label="产品类型" align="center" prop="productType" />
          <el-table-column label="产品仓库" align="center" prop="warehouseName" />
          <el-table-column label="库存" align="center" prop="quantity" />
          <el-table-column label="领用数量" align="center" prop="warehouseName" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" placeholder="领用数量"  style="width: 80px;"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center"  >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="deleteBtn(scope)"
              >删除</el-button>
            </template>
          </el-table-column>

        </el-table>

        <el-form-item label="领用时间" prop="collectingTime">
          <el-date-picker clearable
                          style="width: 200px;"
            v-model="form.collectingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择领用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width: 200px;" type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <importUser ref="importCustomer" @showUser="showCustomer"></importUser>
    <importProduct ref="importProduct" @showProduct="showProduct"></importProduct>
    <!-- 出库详情   -->
    <el-dialog title="出库详情" :visible.sync="outboundOpen" width="50%" append-to-body>
      <el-table :border="true"  :data="detailsList" >
        <el-table-column label="产品名称" align="center" prop="productName"  />
        <el-table-column label="产品类型" align="center" prop="productType"  />
        <el-table-column label="数量" align="center" prop="number"  />
        <el-table-column label="仓库" align="center" prop="warehouseName"  />
      </el-table>

    </el-dialog>
  </div>
</template>

<script>
import { listOutbound, getOutbound, delOutbound, addOutbound, updateOutbound } from "@/api/module/outbound";
import {listWarehouse} from "@/api/module/warehouse";
import  importUser from '@/views/module/common/user'
import  importProduct from '@/views/module/outbound/product'

export default {
  name: "Outbound",
  components:{
    importUser,
    importProduct
  },
  data() {
    return {
      outboundOpen:false,
      detailsList:[],//详情出库记录
      //类型 是领取人还是负责人
      type:'',
      bizOutboundProduct:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库记录表格数据
      outboundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiverId: null,
        receiverName: null,
        responsibleUser: null,
        responsibleUserName: null,
        warehouseId: null,
        warehouseName: null,
        collectingTime: null,
      },
      // 表单参数
      warehouseList:[],
      form: {},
      columns: [
        { key: 0, label: `领用人姓名`, visible: true },
        { key: 1, label: `负责人姓名`, visible: true },
        { key: 2, label: `领用时间`, visible: true },
        { key: 3, label: `备注`, visible: true },
        { key: 4, label: `查看详情`, visible: true },


      ],
      // 表单校验
      rules: {
        receiverName: [
          { required: true, message: '请输入领取人', trigger: 'blur' },
        ],
        responsibleUserName: [
          { required: true, message: '请输入负责人', trigger: 'blur' },
        ],
        // warehouseName: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ],
        collectingTime: [
          { required: true, message: '请输入领取时间', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    this.getList();
    // this.WarehouseList()
  },
  methods: {
    deleteBtn(value){
      // console.log(value)
      // console.log(value.$index)
      this.form.bizOutboundProductList.splice(value.$index,1)
    },
    details(value){
      // console.log(value)
      this.detailsList=value.bizOutboundProductList
      this.outboundOpen=true
    },
    /** 选择领取的物品*/
    handleProduct(){
      this.$refs.importProduct.show()
    },
    showProduct(val){
      console.log(val)
      // this.bizOutboundProduct=val
      this.form.bizOutboundProductList=val
    },
    /** 选择人员*/
    handleStaff(value){
      // console.log(value)
      this.type=value
      this.$refs.importCustomer.show();
    },
    showCustomer(value){
      console.log(value)
      if(this.type==0){
        /** 领用人*/
        this.form.receiverId=value.userId
        this.form.receiverName=value.userName
      }else if(this.type==1){
        /** 负责人*/
        this.form.responsibleUser=value.userId
        this.form.responsibleUserName=value.userName
      }
    },
    /** 选择仓库 */
    changeWarehouse(value){
      // console.log(value)
     // 截取前面的字符串
     var  tr = value.match(/(\S*),/)[1];
      // console.log(tr)
      // 截取后面的字符串
      var  str = value.match(/,(\S*)/)[1];
      // console.log(str)
      this.form.warehouseId=str
      this.form.warehouseName=tr

    },
    /** 查询仓库管理列表 */
    WarehouseList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        console.log(response)
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询出库记录列表 */
    getList() {
      this.loading = true;
      listOutbound(this.queryParams).then(response => {
        this.outboundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receiverId: null,
        receiverName: null,
        responsibleUser: null,
        responsibleUserName: null,
        warehouseId: null,
        warehouseName: null,
        collectingTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        bizOutboundProductList:[]
      };
      this.bizOutboundProduct=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出库记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOutbound(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出库记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOutbound(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutbound(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出库记录编号为"' + ids + '"的数据项？').then(function() {
        return delOutbound(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/outbound/export', {
        ...this.queryParams
      }, `outbound_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
